from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.shortcuts import render
from myblog.models import Comment
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):
		all_comments = Comment.objects.all()
		paginator = Paginator( all_comments, 5)
		page_number = request.GET.get('page')
		try:
			current_page = paginator.page( page_number )
		except PageNotAnInteger:
			current_page = paginator.page( 1 )
		except EmptyPage:
			current_page = paginator.page( paginator.num_pages )
			
		return render(request, 'myblog/index.html', \
						{'current_page':current_page}
					)

def addcomment(request):
	if request.session.get('has_commented',False):
		return HttpResponse('you have commented')
	new_comment = Comment( text = request.POST['comment'] )
	new_comment.save()
	request.session['has_commented'] = True
	return HttpResponseRedirect( reverse('blog:index') )

